/*
 * Copyright 2017 Rainbow.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.seari.semodbus.server;

import com.ghgande.j2mod.modbus.Modbus;
import com.ghgande.j2mod.modbus.io.AbstractModbusTransport;
import com.ghgande.j2mod.modbus.io.ModbusRTUTCPTransport;
import com.ghgande.j2mod.modbus.io.ModbusTCPTransport;
import com.ghgande.j2mod.modbus.net.TCPSlaveConnection;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author Rainbow
 */
public class SETCPSlaveConnection extends TCPSlaveConnection
{
    
    private static final Logger logger = LoggerFactory.getLogger(SETCPSlaveConnection.class);

     public SETCPSlaveConnection(Socket socket, boolean useRtuOverTcp)
    {
        super(socket, useRtuOverTcp);
//        try
//        {
//            setSocket(socket, useRtuOverTcp);
//        } catch (IOException ex)
//        {
//            logger.debug("TCPSlaveConnection::Socket invalid");
//
//            throw new IllegalStateException("Socket invalid", ex);
//        }
    }

    @Override
    public void setSocket(Socket socket, boolean useRtuOverTcp) throws IOException
    {
        super.socket = socket;

        if (transport == null) {
            if (useRtuOverTcp) {
                logger.trace("setSocket() -> using RTU over TCP transport.");
                transport = new ModbusRTUTCPTransport(socket);
            }
            else {
                logger.trace("setSocket() -> using standard TCP transport.");
                transport = new SEmodbusTCPTransport(socket);
            }
        }
        else {
            transport.setSocket(socket);
        }

        connected = true;
    }
    
    
}
